<template>
    <div class="search-div">


        <!-- 添加按钮 -->
        <div class="tools-div">
            <el-button type="primary" size="small" @click="importDataRef.show()">导入数据</el-button>
            <el-button type="primary" size="small" @click="exportData">导出数据</el-button>
            <el-button type="success" size="small" @click="addModelRef.show()">添 加</el-button>
        </div>

        <!--- 角色表格数据 -->
        <!---懒加载的树形表格-->
        <el-table :data="list" style="width: 100%" row-key="id" border
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column prop="name" label="分类名称" />
            <el-table-column prop="imageUrl" label="图标" #default="scope">
                <img :src="scope.row.imageUrl" width="50" />
            </el-table-column>
            <el-table-column prop="orderNum" label="排序" />
            <el-table-column prop="status" label="状态" #default="scope">
                {{ scope.row.status == 1 ? '正常' : '停用' }}
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" />
            <el-table-column prop="updateTime" label="更新时间" />
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="editModelRef.show(scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>

        <!--分页条-->
        <!--分页条-->

    </div>
    <add-model ref="addModelRef" @refresh="fetchData" />
    <edit-model ref="editModelRef" @refresh="fetchData" />
    <import-data ref="importDataRef" @refresh="fetchData" />
</template>

<script setup>
import { ref } from 'vue';
import importData from './components/import-data.vue';
import addModel from './components/add-model.vue';
import editModel from './components/edit-model.vue';

import { findAllNodesApi, exportCategoryDataApi, removeCategoryApi } from '@/api/category'
const addModelRef = ref()
const queryDto = ref()
const editModelRef = ref()
const importDataRef = ref()

// 分页条总记录数
let total = ref(0)
const pageParams = ref({
    pageNum: 1,
    pageSize: 10
})
// 定义表格数据模型
let list = ref([])

// 搜索按钮点击事件处理函数
const searchSysRole = () => {
    //queryDto.value.roleName = ""
    fetchData();
}
// 远程调用后端分页查询接口
const fetchData = async () => {

    const { data } = await findAllNodesApi();
    list.value = data || []

}
// 导出按钮点击事件处理函数
const exportData = () => {
    // 调用 ExportCategoryData() 方法获取导出数据
    exportCategoryDataApi().then(res => {
        // 创建 Blob 对象，用于包含二进制数据
        const blob = new Blob([res]);
        // 创建 a 标签元素，并将 Blob 对象转换成 URL
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        // 设置下载文件的名称
        link.download = '分类数据.xlsx';
        // 模拟点击下载链接
        link.click();
    })
}
// 导入

const handleDelete = (id) => {
    ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        // 确定
        removeCategoryApi(id).then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功!'
            })
            fetchData()
        })
    })
}

fetchData()

</script>

<style scoped>
.search-div {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}

.tools-div {
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}
</style>